package com.yuandian.management.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.yuandian.management.mapper.FlowOriginalDataFilterConfigMapper;
import com.yuandian.management.service.FlowOriginalDataFilterConfigService;
import com.yuandian.management.vo.flow.FilterData;
import com.yuandian.management.vo.flow.FlowOriginalDataFilterConfig;
import com.yuandian.management.vo.flow.OriginalDataFilterEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author lijunhang
 * @date 2023年06月06日 18:28
 * @description 原始数据包捕获配置
 */
@Service
public class FlowOriginalDataFilterConfigServiceImpl implements FlowOriginalDataFilterConfigService {

	@Autowired
	private FlowOriginalDataFilterConfigMapper flowOriginalDataFilterConfigMapper;
	@Override
	public FlowOriginalDataFilterConfig queryOriginalDataFilterList() {
		FlowOriginalDataFilterConfig originalDataHotConfig=new FlowOriginalDataFilterConfig();

		List<OriginalDataFilterEntity> originalDataFilterEntityList = flowOriginalDataFilterConfigMapper.queryOriginalDataFilterList();

		if (!CollectionUtil.isEmpty(originalDataFilterEntityList)) {
			List<Integer> filterIds = originalDataFilterEntityList.stream().map(OriginalDataFilterEntity::getId).collect(Collectors.toList());
			List<FilterData> filterDataList = flowOriginalDataFilterConfigMapper.countFilterDataGroupByFilterIds(filterIds);
			if (!CollectionUtil.isEmpty(filterDataList)) {
				Map<Integer, Integer> filterIdAndCount = filterDataList.stream().collect(Collectors.toMap(FilterData::getFilterId, FilterData::getCount));
				for (OriginalDataFilterEntity entity : originalDataFilterEntityList) {
					if (filterIdAndCount.get(entity.getId()) != null) {
						entity.setCapturedCount(filterIdAndCount.get(entity.getId()));
					} else {
						entity.setCapturedCount(0);
					}
				}
			}

		}
		originalDataHotConfig.setOriginalDataHotConfigList(originalDataFilterEntityList);
		return originalDataHotConfig;
	}
}
